package org.footito.android.adapter;

import org.footito.android.R;
import org.footito.android.data.Event;
import org.footito.android.data.Group;
import org.footito.android.data.TeamStatus;
import org.footito.android.data.database.ProviderAdapter;
import org.footito.android.tools.ConvertingTools;
import org.footito.android.tools.Log;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CursorTimelineAdapter extends CursorAdapter {

	private LayoutInflater inflater;
	private ProviderAdapter providerAdapter;
	private static String JOINGROUP;
	private static String ACTORS_COMS;
	private static String ACTORS_SUBS;

	public CursorTimelineAdapter(Context context, Cursor c) {
		super(context, c);
		init(context);
	}

	public CursorTimelineAdapter(Context context, Cursor c, boolean autoRequery) {
		super(context, c, autoRequery);
		init(context);
	}

	private void init(Context context) {
		this.providerAdapter = new ProviderAdapter(context);
		inflater = LayoutInflater.from(context);
		JOINGROUP = context.getResources().getString(R.string.joinGroup);
		ACTORS_COMS = context.getResources().getString(R.string.comments);
		ACTORS_SUBS = context.getResources().getString(R.string.subscribers);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		MyTag holder = (MyTag) view.getTag();

		if (view.getVisibility() == View.GONE)
			view.setVisibility(View.VISIBLE);

		try {
			Event currentEvent = providerAdapter.getEventFromCursor(cursor);
			if (currentEvent instanceof Group) {
				holder.layoutgroup.setVisibility(View.VISIBLE);
				holder.layoutstatus.setVisibility(View.GONE);

				holder.group.setText(JOINGROUP + " : \"" + currentEvent.getContent() + "\"");
				holder.actorsnum.setText(Integer.toString(((Group) currentEvent).getSubscribers().size()));
				holder.actors.setText(ACTORS_SUBS);
				holder.teamName.setText(currentEvent.getTeam().getName());
			} else if (currentEvent instanceof TeamStatus) {
				holder.layoutgroup.setVisibility(View.GONE);
				holder.layoutstatus.setVisibility(View.VISIBLE);

				holder.teamStatus.setText(Html.fromHtml("<b>" + currentEvent.getTeam().getName() + "</b> : " + currentEvent.getContent()));
				holder.actorsnum.setText(Integer.toString(((TeamStatus) currentEvent).getComments().size()));
				holder.actors.setText(ACTORS_COMS);
			}

			holder.timestamp.setText(ConvertingTools.getFormattedDate(currentEvent.getTimestamp()));

			Bitmap bitmap = currentEvent.getTeam().getPicture(context);
			// if (holder.flag.getTag() == null) {
			// if (bitmap != null) {
			holder.flag.setImageBitmap(bitmap);
			holder.flag.setTag("ok");
			// }
			// }
		} catch (Exception e) {
			Log.e("Exception: " + e.getClass().toString() + ": " + e.getMessage());
			view.setVisibility(View.GONE);
		}

	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		MyTag holder = new MyTag();

		View view = inflater.inflate(R.layout.row_timeline, null);

		try {
			holder.group = (TextView) view.findViewById(R.id.TV_Group);
			holder.teamName = (TextView) view.findViewById(R.id.TV_Team_Name);
			holder.actors = (TextView) view.findViewById(R.id.tv_actors);
			holder.actorsnum = (TextView) view.findViewById(R.id.tv_actorsnum);
			holder.teamStatus = (TextView) view.findViewById(R.id.TV_Team_Status);
			holder.timestamp = (TextView) view.findViewById(R.id.TV_date);
			holder.flag = (ImageView) view.findViewById(R.id.IV_Flag);
			holder.layoutstatus = view.findViewById(R.id.layoutstatus);
			holder.layoutgroup = view.findViewById(R.id.layoutgroup);
		} catch (Exception e) {
			Log.e("Exception: " + e.getClass().toString() + ": " + e.getMessage());
			view.setVisibility(View.GONE);
		}
		view.setTag(holder);

		// Event currentEvent = tempCreate(cursor);
		// if (currentEvent instanceof Group) {
		// holder.layoutgroup.setVisibility(View.VISIBLE);
		// holder.layoutstatus.setVisibility(View.GONE);
		//
		// holder.group.setText(JOINGROUP + " : \"" + currentEvent.getContent()
		// + "\"");
		// holder.actorsnum.setText(Integer.toString(((Group)
		// currentEvent).getSubscribers().size()));
		// holder.actors.setText(ACTORS_SUBS);
		// holder.teamName.setText(currentEvent.getTeam().getName());
		// } else if (currentEvent instanceof TeamStatus) {
		// holder.layoutgroup.setVisibility(View.GONE);
		// holder.layoutstatus.setVisibility(View.VISIBLE);
		//
		// holder.teamStatus.setText(Html.fromHtml("<b>" +
		// currentEvent.getTeam().getName() + "</b> : " +
		// currentEvent.getContent()));
		// holder.actorsnum.setText(Integer.toString(((TeamStatus)
		// currentEvent).getComments().size()));
		// holder.actors.setText(ACTORS_COMS);
		// }
		//
		// holder.timestamp.setText(ConvertingTools.getFormattedDate(currentEvent.getTimestamp()));
		//
		// Bitmap bitmap = currentEvent.getTeam().getPicture(context);
		// // if (holder.flag.getTag() == null) {
		// // if (bitmap != null) {
		// holder.flag.setImageBitmap(bitmap);
		// holder.flag.setTag("ok");
		// // }
		// // }

		return view;
	}

	private class MyTag {
		TextView actorsnum;
		ImageView flag;
		TextView teamStatus;
		TextView group;
		TextView teamName;
		TextView timestamp;
		TextView actors;
		View layoutgroup;
		View layoutstatus;
	}
}
